package cn.cqvie.controller;

import cn.cqvie.common.R;
import cn.cqvie.dto.CakeDto;
import cn.cqvie.pojo.Cake;
import cn.cqvie.pojo.Category;
import cn.cqvie.pojo.Employee;
import cn.cqvie.service.CakeService;
import cn.cqvie.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cake")
public class CakeController {
    @Autowired
    private CakeService cakeService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/getMSG")
    public R<Page> getPage(int page,int pageSize,String name){
        Page<Cake> myPage= new Page<>(page,pageSize);

        Page<CakeDto> dtoPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Cake> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Cake::getName,name);
        queryWrapper.orderByDesc(Cake::getCreateTime);
        cakeService.page(myPage,queryWrapper);

        BeanUtils.copyProperties(myPage,dtoPage,"records");
        List<CakeDto> dtos=myPage.getRecords().stream().map(item->{
            CakeDto cakeDto=new CakeDto();
            System.out.println(cakeDto);
            BeanUtils.copyProperties(item,cakeDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            cakeDto.setCategoryName(category.getName());
            return cakeDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(dtos);
        return R.success(dtoPage);
    }
    @PutMapping("/editCake")
    public R<String> editStatus(@RequestBody Cake cake){
        Cake c = cakeService.getById(cake.getId());
        cakeService.updateById(cake);
        return R.success("修改成功");
    }
    @PostMapping("/submit")
    public R<String> submitCake(@RequestBody Cake cake){
        Employee e =(Employee) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        cake.setCreateUser(e.getId());
        cake.setUpdateUser(e.getId());
        cakeService.save(cake);
        return R.success("添加成功");
    }
    @DeleteMapping("/deleteCake")
    public R<String> deleteCake(Long id){
        Cake cake = cakeService.getById(id);
        if(cake.getStatus()==1){
            return R.error("该商品正在售卖中，不能删除");
        }
        cakeService.removeById(id);
        return R.success("删除成功");
    }
    @GetMapping("/getCake")
    public R<Cake> getCakeInfo(Long id){
        Cake cake = cakeService.getById(id);
        return R.success(cake);
    }
    @GetMapping("/getNumber")
    public R<List<Map<String,Object>>> getNumber(){
        QueryWrapper<Cake> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("category_id").select("category_id as cid,count(*) as num");
        queryWrapper.orderByDesc("category_id");
        List<Map<String, Object>> count = cakeService.listMaps(queryWrapper);
        return R.success(count);
    }
}
